package server;

import java.util.LinkedList;

public class Handler implements Runnable {

	private TagHandlerList list;
	private ParsedTweetStack stack;
	private TweetParser parser;
	private ClientManager manager;

	public void setManager(ClientManager manager) {
		this.manager = manager;
	}

	public ClientManager getManager() {
		return manager;
	}

	public void setParser(TweetParser parser) {
		this.parser = parser;
	}

	public TweetParser getParser() {
		return parser;
	}

	Handler() {
		this.list = new TagHandlerList();
		this.stack = new ParsedTweetStack();
	}

	public ParsedTweetStack getStack() {
		return stack;
	}

	public void addDispatcher(DispatcherInfo d) {
		System.out.println("Some one added a new dispatcher");
		LinkedList<String> tags = d.getTags();
		System.out.println("Dispatcher tags: " + tags);
		int size = tags.size();
		tagsint: for (int i = 0; i < size; i++) {
			String tag = tags.pop();
			if (list.isEmpty()) {
				System.out.println("Empty taghandler list, creating a new one");
				newTagThread(d, tag);
			} else {
				int max = list.size();
				for (int j = 0; j < max; j++) {
					System.out.println(tag + " == " + list.get(j).getTag());
					if (tag.toLowerCase().equals(
							list.get(j).getTag().toLowerCase())) {
						System.out
								.println("A taghandler allready exists no need to create a new one");
						((list.get(j)).getList()).add(d);
						continue tagsint;
					}
				}
				newTagThread(d, tag);
			}
		}
	}

	public void removeTag(TagHandler t) {
		int size = list.size();
		for (int i = 0; i < size; i++) {
			if (list.get(i) == t) {
				System.out.println("Removing tag from list");
				list.remove(i);
				getManager().finishTag(t.getTag());
				break;
			}
		}

	}

	public void newTagThread(DispatcherInfo d, String tag) {
		System.out.println("Adding tag " + tag + " to handler list");
		TweetStack stack = new TweetStack();
		TagHandler t = new TagHandler(stack, d, tag, this);
		new Thread(t).start();
		list.add(t);
	}

	@Override
	public void run() {
		while (true) {
			if (stack.isEmpty()) {
				try {
					System.out.println("Lista de Parsedtweets vazia");
					//Thread.sleep(10000);
					synchronized (stack){
						stack.wait();
					}
				} catch (InterruptedException e) {
					continue;
				}
			} else {
				System.out.println("Got a Parsed tweet!");

				ParsedTweet tweet = stack.pop();
				LinkedList<String> tags = tweet.getTags();
				int tagssize = tags.size();
				TagHandlerList intlist = new TagHandlerList(list);
				int handlersize = intlist.size();
				for (int i = 0; i < handlersize; i++) {
					TagHandler t = intlist.get(i);
					if (t.isAlive()) {
						for (int j = 0; j < tagssize; j++) {
							if (t.getTag().equalsIgnoreCase(tags.get(j))) {
								System.out.println("Sent a tweet "
										+ tweet.getTweet().getId() + " to the "
										+ t.getTag() + " tag handler");
								(t.getStack()).push(tweet.getTweet());
							}
						}
					} else {
						System.out.println("A tag is no more.");
						this.removeTag(t);
					}
				}
			}
		}
	}

}
